/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bio.jpa.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Diego
 */
@Entity
@Table(name = "gen_bank", catalog = "biodb", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "GenBank.findAll", query = "SELECT g FROM GenBank g"),
    @NamedQuery(name = "GenBank.findByIdGen", query = "SELECT g FROM GenBank g WHERE g.idGen = :idGen"),
    @NamedQuery(name = "GenBank.findByVersion", query = "SELECT g FROM GenBank g WHERE g.version = :version"),
    @NamedQuery(name = "GenBank.findByDefinition", query = "SELECT g FROM GenBank g WHERE g.definition = :definition"),
    @NamedQuery(name = "GenBank.findByCreationDate", query = "SELECT g FROM GenBank g WHERE g.creationDate = :creationDate")})
public class GenBank implements Serializable {
    @Basic(optional =     false)
    @NotNull
    @Column(name = "creation_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Basic(optional = false)
    @Column(name = "id_gen")
    private Integer idGen;
    @Size(max = 45)
    @Column(name = "version")
    private String version;
    @Size(max = 160)
    @Column(name = "definition")
    private String definition;
    @JoinTable(name = "gen_bank_has_audit_log", joinColumns = {
        @JoinColumn(name = "gen_bank_id_gen", referencedColumnName = "id_gen")}, inverseJoinColumns = {
        @JoinColumn(name = "audit_log_id_log", referencedColumnName = "id_log")})
    @ManyToMany
    private List<AuditLog> auditLogList = new ArrayList<AuditLog>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "genBankIdGen")
    private List<Contig> contigList = new ArrayList<Contig>();;
    @JoinColumn(name = "created_by", referencedColumnName = "user_id")
    @ManyToOne
    private User createdBy;
    @JoinColumn(name = "prj_fk", referencedColumnName = "prj_id")
    @ManyToOne(optional = false)
    private Project prjFk;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "genFk")
    private List<Sequence> sequenceList = new ArrayList<Sequence>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "genFk")
    private List<Feature> featureList = new ArrayList<Feature>();

    public GenBank() {
    }

    public GenBank(Integer idGen) {
        this.idGen = idGen;
    }

    public GenBank(Integer idGen, Date creationDate) {
        this.idGen = idGen;
        this.creationDate = creationDate;
    }

    public Integer getIdGen() {
        return idGen;
    }

    public void setIdGen(Integer idGen) {
        this.idGen = idGen;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getDefinition() {
        return definition;
    }

    public void setDefinition(String definition) {
        this.definition = definition;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @XmlTransient
    public List<AuditLog> getAuditLogList() {
        return auditLogList;
    }

    public void setAuditLogList(List<AuditLog> auditLogList) {
        this.auditLogList = auditLogList;
    }

    @XmlTransient
    public List<Contig> getContigList() {
        return contigList;
    }

    public void setContigList(List<Contig> contigList) {
        this.contigList = contigList;
    }

    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public Project getPrjFk() {
        return prjFk;
    }

    public void setPrjFk(Project prjFk) {
        this.prjFk = prjFk;
    }

    @XmlTransient
    public List<Sequence> getSequenceList() {
        return sequenceList;
    }

    public void setSequenceList(List<Sequence> sequenceList) {
        this.sequenceList = sequenceList;
    }

    @XmlTransient
    public List<Feature> getFeatureList() {
        return featureList;
    }

    public void setFeatureList(List<Feature> featureList) {
        this.featureList = featureList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idGen != null ? idGen.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GenBank)) {
            return false;
        }
        GenBank other = (GenBank) object;
        if ((this.idGen == null && other.idGen != null) || (this.idGen != null && !this.idGen.equals(other.idGen))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.bio.jpa.entities.GenBank[ idGen=" + idGen + " ]";
    }

    @Transient
    public Integer getSequenceSize() {
        Integer r = 0;
        for (Sequence s : sequenceList) {
            r += s.getSeq().length();
        }
        return r;
    }
}
